#include "StdAfx.h"
#include "sys.h"
#include "io.h"
#include "str.h"



bool sys::findFile( const TCHAR*fileName,CString&path )
{
	path=fileName;
	if(0==_taccess(path,0))
	{
		return true;
	}
	path=getAppPath();
	path+=fileName;
	if(0==_taccess(path,0))
	{
		return true;
	}
	static TCHAR buf[4096];
	DWORD rt=GetEnvironmentVariable(_T("PATH"),buf,_countof(buf));
	if(rt>0)
	{
		SplitString sp(buf,_T(";"));
		int num=sp.m_arrStr.size();
		for(int i=0;i<num;++i)
		{
			path=sp.m_arrStr[i];
			path+=_T("\\");
			path+=fileName;
			if(0==_taccess(path,0))
			{
				return true;
			}
		}
	}
	return false;
}

CString sys::getAppPath()
{
	TCHAR path[MAX_PATH];
	GetModuleFileName(NULL, path, MAX_PATH);
	PathRemoveFileSpec(path);
	_tcscat_s(path, _T("\\"));
	return path;
}
